package com.qingxin.wuye.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.service.SysUserService;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.SysUserBuildingRel;
import com.qingxin.wuye.entity.WyBuilding;
import com.qingxin.wuye.entity.WyGarden;

@Service
public class SysUserBuildingRelService extends BaseService<SysUserBuildingRel, String> {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserGardenRelService sysUserGardenRelService;
	
	
	/**
	 * 添加用户-楼宇关联
	 * @param userid
	 * @param buildingid
	 * @param loginUser
	 * @return
	 */
	public SysError addUserBuildingRel(String userid, String buildingid, LoginUser loginUser){
		if(StringUtils.isEmpty(userid) || StringUtils.isEmpty(buildingid)){
			return null;
		}
		Object id = getByHql("select id from SysUserBuildingRel where sysUser.userid=? and wyBuilding.buildingid=?", userid, buildingid);
		if(id != null){
			return null;
		}
		
		SysUserBuildingRel rel = new SysUserBuildingRel(new WyBuilding(buildingid), new SysUser(userid), loginUser.getLogin_name(), new Date());
		this.save(rel);
		
		return null;
	}
	
	
	/**
	 * 设置用户-楼宇查看权限
	 * @param userid
	 * @param buildingids
	 * @param loginUser
	 * @return
	 */
	public SysError saveUserBuildingRels(String userid, String[] buildingids, LoginUser loginUser){
		if(StringUtils.isEmpty(userid) ){
			return null;
		}
		//删除现有关联
		deleteByUserid(userid);
		
		if(buildingids==null || buildingids.length==0){
			return null;
		}
		
		Date now = new Date();
		String createBy = loginUser.getLogin_name();
		
		for(String buildingid : buildingids){
			SysUserBuildingRel rel = new SysUserBuildingRel(new WyBuilding(buildingid), new SysUser(userid), createBy, now);
			this.save(rel);
		}
		
		return null;
	}
	
	
	/**
	 * 获取指定用户关联的楼宇id集合
	 * @param userid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Set<String> getUserBuildingids(String userid){
		Set<String> set = new HashSet<>();
		if(StringUtils.isEmpty(userid) ){			
			return set;
		}
		
		List<String> list = listByHql("select wyBuilding.buildingid from SysUserBuildingRel where sysUser.userid=?", userid);
		if(list != null){
			set.addAll(list);
		}
		
		return set;
	}
	
	
	
	/**
	 * 删除用户-楼宇关联
	 * @param userid
	 * @return
	 */
	public Object deleteByUserid(String userid){
		deleteByHql("delete from SysUserBuildingRel where sysUser.userid=?", userid);
		return null;
	}
	
	
	
	/**
	 * 设置用户-园区-楼宇关联关系（数据权限）的数据
	 * @param userid
	 * @param loginUser
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Object getSetGardenData(String userid, LoginUser loginUser){
		List<Map<String, String>> list = new ArrayList<>();
		Set<String> rels = new HashSet<>();
		rels.addAll(getUserBuildingids(userid));
		rels.addAll(sysUserGardenRelService.getUserGardenids(userid));
		
		SysUser user = sysUserService.get(userid);
		
		List<WyGarden> gardens = listByHql("select new com.qingxin.wuye.entity.WyGarden(p.gardenid, p.gardenName)" +
				" from WyGarden p where p.wyPropertyCompany.companyid='"+user.getCompanyid()+"'" +
				" order by p.gardenName asc");
		if(gardens != null){
			for(WyGarden garden : gardens){
				Map<String, String> node = new HashMap<>();
				node.put("id", garden.getGardenid());
				node.put("name", garden.getGardenName());
				node.put("pid", "0");
				node.put("hasRights", ""+rels.contains(garden.getGardenid()));
				node.put("datatype", "garden");
				list.add(node);
			}
		}
		List<WyBuilding> buildings = listByHql("select new com.qingxin.wuye.entity.WyBuilding(b.buildingid, b.buildingName, p.gardenid)" +
				" from WyBuilding b,WyGarden p where b.wyGarden.gardenid=p.gardenid and p.wyPropertyCompany.companyid='"+user.getCompanyid()+"'" +
				" order by b.buildingName asc");
		if(buildings != null){
			for(WyBuilding building : buildings){
				Map<String, String> node = new HashMap<>();
				node.put("id", building.getBuildingid());
				node.put("name", building.getBuildingName());
				node.put("pid", building.getWyGarden().getGardenid());
				node.put("hasRights", ""+rels.contains(building.getBuildingid()));
				node.put("datatype", "building");
				list.add(node);
			}
		}
		
		return list;
	}
	
	
	
}
